<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<meta name="renderer" content="webkit|ie-comp|ie-stand">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
<!--[if lt IE 9]>
<script type="text/javascript" src="lib/html5.js"></script>
<script type="text/javascript" src="lib/respond.min.js"></script>
<script type="text/javascript" src="lib/PIE_IE678.js"></script>
<![endif]-->
<link href="css/H-ui.min.css" rel="stylesheet" type="text/css" />
<link href="css/H-ui.admin.css" rel="stylesheet" type="text/css" />
<link href="lib/icheck/icheck.css" rel="stylesheet" type="text/css" />
<link href="lib/Hui-iconfont/1.0.7/iconfont.css" rel="stylesheet" type="text/css" />
<link href="lib/webuploader/0.1.5/webuploader.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="lib/jquery/1.9.1/jquery.min.js"></script> 
<script type="text/javascript" src="lib/webuploader/0.1.5/webuploader.min.js"></script> 
<script type="text/javascript" src="lib/layer/2.1/layer.js"></script> 
<script type="text/javascript" src="js/H-ui.js"></script> 
<script type="text/javascript" src="js/s.js"></script> 
<script type="text/javascript" src="lib/ueditor/1.4.3/ueditor.config.js"></script> 
<script type="text/javascript" src="lib/ueditor/1.4.3/ueditor.all.min.js"> </script> 
<script type="text/javascript" src="lib/ueditor/1.4.3/lang/zh-cn/zh-cn.js"></script> 
<!--[if IE 6]>
<script type="text/javascript" src="lib/DD_belatedPNG_0.0.8a-min.js" ></script>
<script>DD_belatedPNG.fix('*');</script>
<![endif]-->
<title>新增文章</title>
<style type="text/css">
li{list-style: none;}
.uploader-list{display: block;}
.uploader-list .item{display: inline-block; margin-right: 7px; text-align: center; cursor: pointer;position: relative;  float: left;}
.uploader-list .item .pic-box{position: relative;border: #f0ecec 1px solid; width: 85px; height: 85px;}
.uploader-list .item i{position: absolute; right: 2px; top: 53px; font-size: 24px; font-weight: 500;}

.uploader-list .item .pic-box img{width: 85px; height: 85px;}
.uploader-list .item .info{max-width: 85px; font-size: 12px; max-height: 40px; overflow: hidden; white-space: nowrap; text-overflow: ellipsis;}

.progress-box{position: absolute; bottom: 0px; text-align: left; width: 100%; height: 85px;background-color: #000; width: 100%; opacity: 0.4;}

.upload-state-error{color: #ff0000;}

#imageList{width: 100%; min-height: 350px; display: block;}
#imageList li{width: 85px; height: 85px; list-style: none; position: relative; margin: 5px 4px; cursor: pointer; float: left; border:#f0ecec 1px solid; }
#imageList li img{width: 85px; height: 85px;}
#imageList li i{position: absolute; right: 0px; bottom: -5px; display: none;}
#imageList li span{display: none;}
.HuiTab{width: 100%; min-height: 300px; height: auto;}
.tabCon{width: 98%; display: block; padding: 1%;}
</style>
</head>
<body>
<div class="pd-10">
    <div id="tab_demo" class="HuiTab">
        <div class="tabBar cl" style="position:fixed; top:0px; width:97%;">
            <!-- <span>在线管理(升级中)</span> -->
            <span>本地上传</span>
            
        </div>
        <div class="tabCon" style="border-top:transparent 20px solid;">
            <!-- 在线图片 -->
            <div id="online" class="panel">
                <div id="imageList">
                   
                </div>
            </div>
        </div>
        <div class="tabCon pd-20 text-c">
            <div class="uploader-thum-container text-c">    
                <div id="filePicker" class="r mr-10" style="display: inline-block; position: absolute; top: 0px; right: 10px;">选择文件</div>
                <div class="mr-10 text-r" style="color: #999;font-size: 12px;line-height: 30px; border-top:transparent 18px solid" id="showtypeinfo">
                    图片：jpg, jpeg, gif, png, bmp
                </div>
                <div id="fileList" class="uploader-list" style="display:block; overflow-y: scroll; height: 345px;"></div>
            </div>   
            
        </div>
    </div>
        
      
    <div class="mt-10" style="display: block; position: absolute; bottom: 10px; right: 10px;">   
        <span class="btn btn-default radius r mr-10" id="tijiao" style="height: 32px; font-size: 14px;">确定</span> 

        <!-- <span class="btn btn-primary radius r mr-10" id="tijiao" style="height: 32px; font-size: 14px;">确定</span>  -->
        <span class="btn btn-default radius r mr-10" id="close" style="height: 32px; font-size: 14px;">取消</span>
    </div>    
       
</div>





<script type="text/javascript">

var ossurl;
var issubmit=false;

$(function(){

    $.Huitab("#tab_demo .tabBar span","#tab_demo .tabCon","current","click","1");

    

    // 取消
    $(document).on('click','span#close',function(){
        var index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(index); 
    })

    


    var type=getUrlParam('type') || 'image';//上传类型图片image或者文件file
    console.log(type)
    var api=getUrlParam('api') || 'article';//接口
    var single=getUrlParam('single') || '0';//0多图或多文件上传，1单图或单文件上传
    var idname=getUrlParam('idname');//容器id
    var inputname=getUrlParam('inputname');//保存字段的name
    if(idname==null || inputname==null){
        parent.parent.layer.msg("参数缺失", {icon:2,time:2000});
        return;
    }


    if(type=="ueditor"){
        console.log("000")
        // 上传文件类型
        var extensions="gif,jpg,jpeg,bmp,png";
        var mimeTypes="image/*"
        var url=API_IMGUPLOAD;
    }else if(type=="image"){
        // 上传文件类型
        var extensions="gif,jpg,jpeg,bmp,png";
        var mimeTypes="image/*"
        var url=API_IMGUPLOAD;

    }else if(type=="apk"){
        var extensions="apk";
        var mimeTypes="*"
        var url=API_APKUPLOAD;

    }else if(type=="audio"){
        var extensions="mp3";
        var mimeTypes="audio/mpeg";
        var url=API_FILEUPLOAD;
        $("#showtypeinfo").text("支持音频格式：mp3");
    }else if(type="video"){
        var extensions="mp4";
        var mimeTypes="video/mp4";
        var url=API_FILEUPLOAD;
        $("#showtypeinfo").text("支持视频格式：mp4");
    }else{
        var url=API_FILEUPLOAD;

        var extensions="xlsx,xls,doc,docx,ppt,pptx,pdf,rar,zip,jpg,jpeg,gif,png,bmp,txt";
        var mimeTypes="application/msword,application/vnd.ms-excel,application/vnd.ms-powerpoint,application/pdf,text/plain,application/x-rar-compressed,application/zip,image/*,application/vnd.openxmlformats-officedocument.wordprocessingml.document,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.openxmlformats-officedocument.presentationml.presentation"
    }

    // 点击选中事件
    $(document).on('click','li.xuan',function(){
        if($(this).hasClass("checked")==false){
            if(single==1){
                $(".xuan").find("i").hide();
                $(".xuan").removeClass("checked");
            }
            $(this).find("i").show();
            $(this).addClass("checked");
        }else{
            $(this).removeClass("checked");
            $(this).find("i").hide();
        }
    })

    // 选择完成提交关闭对话框
    $(document).on('click','span#tijiao',function(){
        console.log(issubmit)
        if(issubmit){
            var images="";
            $(".tabCon li.checked").each(function(e){
                var savename=$(this).data("savename");
                var savepath=$(this).data("savepath");
                var name=$(this).data("name");
                var size=$(this).data("size");
                var id=$(this).prop("id");
                var icon=$(this).data("icon");
                // debugger;
                if(type=="ueditor"){
                    console.log(ossurl.host);

                    var ue = parent.UE.getEditor('editor');
                    
                    ue.execCommand('inserthtml','<img src='+ossurl.host+'wd_ueditor/'+savepath+'>', true);
                    
                }else if(type=="image"){
                    if(single==1){
                        parent.$(idname).html('<img src="'+icon+'"><i class="icon Hui-iconfont fun-img-del" data-inputname="'+inputname+'" data-id="'+idname+'"></i><input type="hidden" name="'+inputname+'" value="'+savepath+'">');
                    }else{
                        inputname2=inputname+'['+new Date().valueOf()+']';
                        parent.$(idname).append('<li><img src="'+icon+'"><i class="Hui-iconfont" onclick="fj_close(this);">&#xe706;</i><input type="hidden" name="'+inputname2+'" value="'+savepath+'"></li>');
                    }
                    
                }else if(type=="audio"){
                    parent.$(idname).html('<audio controls=true><source src="'+savename+'" /></audio><input type="hidden" name="'+inputname+'" value="'+savename+'"><i class="icon Hui-iconfont mediadel" data-type="audio"></i>');
                    
                }else if(type=="video"){
                    parent.$(idname).html('<video controls="controls" src="'+savename+'" /></video><input type="hidden" name="'+inputname+'" value="'+savename+'"><i class="icon Hui-iconfont mediadel" data-type="video"></i>');
                    
                }else{
                    if(single!='1') inputname2=inputname+'['+new Date().valueOf()+']';
                    parent.$(idname).append('<li><img src="'+icon+'"><i class="Hui-iconfont" onclick="fj_close(this);">&#xe706;</i><input type="hidden" name="'+inputname+'" value="'+savepath+'"></li>');
                }
                   
            });
            var index = parent.layer.getFrameIndex(window.name);
            parent.layer.close(index);
        }
        
    })


    $.getJSON(API_OSSUPLOAD+'?type='+api,function(urldata){
        // 获取上传链接
        ossurl=urldata.datas;
        var randomfiles=[];
        var maxSize=1000;
        // 以下为上传相关函数

        idname='#'+idname; //容器id

        // ossurl.key=ossurl.key+getrandom()+'/'+imgname;
        // ossurl.key=ossurl.key+'11'+'/'+imgname;

        $list = $("#fileList"),
        $btn = $("#btn-star"),
        state = "pending",
        uploader;
        if(single==1){
            var multiple=false;
        }else{
            var multiple=true;
        }
        var uploader = WebUploader.create({
            auto: true,// 打开后自动上传
            swf: 'lib/webuploader/0.1.5/Uploader.swf',
            // 文件接收服务端。
            server: ossurl.host,
            pick: {
                id:'#filePicker',// 选择文件的按钮。可选。
                multiple:multiple,//是否允许多文件上传
            },

            

            //threads {Boolean} [可选] [默认值：3] 上传并发数。允许同时最大上传进程数。
            resize: false,// 不压缩image, 默认如果是jpeg，文件上传前会压缩一把再上传！
            // 设置允许上传的文件格式
            accept: {
                title: 'Images',
                extensions: extensions,
                mimeTypes: mimeTypes
            },
            fileSingleSizeLimit: maxSize * 1024 * 1024,
            // 自定义上传参数
            formData: ossurl
        });
       

        // 当validate不通过时
        uploader.on("error",function (type){
            if (type=="Q_TYPE_DENIED"){
                layer.msg("包含不允许上传的文件类型", {icon:2,time:1000});;
            }else if(type=="F_EXCEED_SIZE"){
                layer.msg("数据大小超过了"+maxSize+"M", {icon:2,time:1000});
            }
            // console.log(type);
        });

        // 文件上传失败，显示上传出错。
        uploader.on( 'uploadError', function( file ) {
            $( '#'+file.id ).addClass('upload-state-error').find(".state").text("上传出错");
        });
        // 开始加入队列
        uploader.on( 'fileQueued', function( file ) {
            var tt=$("#fileList .item").length;
            if(tt>=9){
                layer.msg('最多只能上传9张', {icon:2,time:1000});
                return;
            }
            var $li = $(
                '<li id="' + file.id + '" class="item xuan" title="'+ file.name +'">' +
                    '<div class="pic-box"><img></div>'+
                    // '<i class="Hui-iconfont" style="font-size:24px;">&#xe617;</i>'+  
                    '<div class="info" >' + file.name + '</div>' +
                    '<p class="state">等待上传...</p>'+
                '</li>'

                
            ),
            $img = $li.find('img');
            $list.append( $li );
        
            // 创建缩略图,如果为非图片文件，可以不用调用此方法。
            var thumbnailWidth=85;
            var thumbnailHeight=85;

            uploader.makeThumb( file, function( error, src ) {

                if ( error ) {
                    if(file.ext=="jpg" || file.ext=="bmp" || file.ext=="gif" || file.ext=="png" || file.ext=="jpeg"){
                        src=data.datas.savename;
                    }else{
                        src=getIcon(file.ext);
                    } 
                }
                $img.attr( 'src', src );
                $li.attr("data-icon",src);

            }, thumbnailWidth, thumbnailHeight );
        });

        // 文件上传过程中创建进度条实时显示。
        uploader.on( 'uploadProgress', function( file, percentage ) {
            
            var $li = $( '#'+file.id ),
                $percent = $li.find('.pic-box .progress-box');
                // $percent = $li.find('.state');
        
            // 避免重复创建
            if ( !$percent.length ) {
                $('<div class="progress-box"></div>').appendTo( $li.find('.pic-box') );
                $percent = $li.find('.pic-box .progress-box');
            }
            // $li.find(".state").text("上传中");
            $percent.css( 'height', (1-percentage) * 100 + '%' );
            // console.log(data);
        });
        uploader.on("uploadBeforeSend",function(object,data,headers){
            
            var hash=object.file.__hash;
            console.log(getrandom2())
            var file=getrandom()+'/'+getrandom2()+'.'+object.file.ext;

            data.key+=file;
            // file;

            randomfiles=randomfiles.concat({
                hash:hash,
                file:file,
            });

            

            // data.key=ossurl.key+filename;

        });
        
        // 文件上传成功，给item添加成功class, 用样式标记上传成功。
        uploader.on( 'uploadSuccess', function( file,data ) {
            var savename=''
            var savepath=''
            for(i in randomfiles){
                if(file.__hash==randomfiles[i].hash){
                    savepath=randomfiles[i].file;
                    savename=ossurl.host+'/'+ossurl.key+randomfiles[i].file;

                }
            }
            


            $( '#'+file.id ).addClass('upload-state-success').find(".state").text("已上传");
            $( '#'+file.id ).attr("data-savename",savename);
            // sunny

            $( '#'+file.id ).attr("data-savepath",savepath);
            $( '#'+file.id ).addClass('checked');
            $( '#'+file.id ).append('<i class="Hui-iconfont" style="font-size:24px;">&#xe617;</i>');
            
        });  
        
        // 完成上传完了，成功或者失败，先删除进度条。
        uploader.on( 'uploadComplete', function( file ) {
            $( '#'+file.id ).find('.progress-box').fadeOut();
            console.log("allend")
            issubmit=true;
            $("#tijiao").removeClass("btn-default").addClass('btn-primary');
        });

        uploader.on('all', function (type) {
            if (type === 'startUpload') {
                state = 'uploading';
            } else if (type === 'stopUpload') {
                state = 'paused';
            } else if (type === 'uploadFinished') {
                state = 'done';
            }

            if (state === 'uploading') {
                $btn.text('暂停上传');
            } else {
                $btn.text('开始上传');
            }
        });

        $btn.on('click', function () {
            if (state === 'uploading') {
                uploader.stop();
            } else {
                uploader.upload();
            }
        });
    })




});



</script>
</body>
</html>